/**
 *
 * 修改密码接口
 *
 */
const router = require('express').Router();
const baseTool = require('../../../common/base-tool');
const tips = require('../../../common/tips');
const md5 = require('md5-node');


router.post('/changpwd', async (req, res, next) => {
    console.log('changpwd api login');

    const r = req.body;
    const phone = r.phone;
    const code = r.code;
    const password = r.password;
    const password2 = r.password2;

    if (!phone) {
        console.log(tips.phone.emport);
        res.json({
            code: tips.phone.emptyCode,
            msg: tips.phone.emport
        });
        return;
    }
    if (!baseTool.verifyPhone(phone)) {
        console.log(tips.phone.format);
        res.json({
            code: tips.phone.formatCode,
            msg: tips.phone.format
        });
        return;
    }
    if (!password) {
        console.log(tips.password.emport);
        res.json({
            code: tips.password.emptyCode,
            msg: tips.password.emport
        });
        return;
    }
    if (!password2) {
        console.log(tips.password.emport2);
        res.json({
            code: tips.password.empty2Code,
            msg: tips.password.emport2
        });
        return;
    }
    if (password != password2) {
        console.log(tips.password.alike);
        res.json({
            code: tips.password.alikeCode,
            msg: tips.password.alike
        });
        return;
    }
    if (!code) {
        console.log(tips.code.empty);
        res.json({
            code: tips.code.emptyCode,
            msg: tips.code.empty
        });
        return;
    }
    if (!baseTool.verifyCode(code)) {
        console.log(tips.code.format);
        res.json({
            code: tips.code.formatCode,
            msg: tips.code.format
        });
        return;
    }

    try {
        const login_rows = '';

        if (login_rows.changedRows == 1) {
            res.json({
                code: tips.successCode,
                msg: tips.password.success
            })
        } else {
            res.json({
                code: tips.password.failCode,
                msg: tips.password.fail
            });
        }
    } catch (e) {
        console.log('密码修改失败：' + e);
    }
});

module.exports = router;